package com.lei.s04session.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lei.s04session.service.UserService;
import com.lei.s04session.service.impl.UserServiceImpl;
import com.lei.s04session.util.IpUtil;
import com.lei.sqlGoodsJsp.dao.Impl.JDBC;
import com.lei.sqlGoodsJsp.entity.User;

public class UserServlet extends HttpServlet {
	private UserService userLoginService = new UserServiceImpl();

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		String verifyCode = req.getParameter("verifyCode");
		String sessionVerifyCode = (String) req.getSession().getAttribute("verify");
		// 校验验证码
		if (sessionVerifyCode != null && !sessionVerifyCode.equals(verifyCode)) {
			req.setAttribute("msg", "验证码错误");
			req.getRequestDispatcher("s04session/login.jsp").forward(req, resp);
			return;
		}
		// 校验用户名密码
		if (userLoginService.login(userName, password,IpUtil.getIpAddress(req))) {
			req.getSession().setAttribute("loginInfo", userName);
			resp.sendRedirect("LoginLogServlet?method=list");
		} else {
			req.setAttribute("msg", "账号或密码错误");
			req.getRequestDispatcher("s04session/login.jsp").forward(req, resp);
		}
	}

}
